package com.fr.report.cellElement.core;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.list.IntList;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.condition.Condition;
import com.fr.report.script.Calculator;
import com.fr.report.script.Primitive;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CustomGrouper extends RecordGrouper
{
  public static final int TOGETHER = 0;
  public static final int DISCARD = 1;
  public static final int LEAVE = 2;
  private boolean force = false;
  private ConditionGroup[] conditionGroups;
  private boolean more = true;
  private int other = 0;
  private String odisplay = Inter.getLocText("Other");

  public boolean isForce()
  {
    return this.force;
  }

  public void setForce(boolean paramBoolean)
  {
    this.force = paramBoolean;
  }

  public boolean isMore()
  {
    return this.more;
  }

  public void setMore(boolean paramBoolean)
  {
    this.more = paramBoolean;
  }

  public ConditionGroup[] getConditionGroups()
  {
    return this.conditionGroups;
  }

  public void setConditionGroups(ConditionGroup[] paramArrayOfConditionGroup)
  {
    this.conditionGroups = paramArrayOfConditionGroup;
  }

  public int getOther()
  {
    return this.other;
  }

  public void setOther(int paramInt)
  {
    this.other = paramInt;
  }

  public String getOdisplay()
  {
    return this.odisplay;
  }

  public void setOdisplay(String paramString)
  {
    this.odisplay = paramString;
  }

  public Group[] group(TableData paramTableData, int paramInt, int[] paramArrayOfInt, Calculator paramCalculator)
  {
    Object localObject;
    if (!(VT4FR.ADVANCED_FORMULA.support()))
      return new MonoGrouper().group(paramTableData, paramInt, paramArrayOfInt, paramCalculator);
    if (paramInt == -1)
      return super.group(paramTableData, paramInt, paramArrayOfInt, paramCalculator);
    if (paramArrayOfInt == null)
      try
      {
        paramArrayOfInt = IntList.range(paramTableData.getRowCount());
      }
      catch (TableDataException localTableDataException1)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
      }
    if ((this.conditionGroups == null) || (paramInt < 0) || (paramArrayOfInt.length == 0))
      return new Group[0];
    Object[] arrayOfObject = new Object[this.conditionGroups.length];
    IntList[] arrayOfIntList = new IntList[this.conditionGroups.length];
    for (int i = 0; i < this.conditionGroups.length; ++i)
    {
      arrayOfObject[i] = this.conditionGroups[i].getDisplay();
      arrayOfIntList[i] = new IntList();
    }
    ArrayList localArrayList1 = new ArrayList();
    IntList localIntList = new IntList();
    for (int j = 0; j < paramArrayOfInt.length; ++j)
    {
      k = paramArrayOfInt[j];
      int l = 0;
      for (int i1 = 0; i1 < this.conditionGroups.length; ++i1)
      {
        ConditionGroup localConditionGroup = this.conditionGroups[i1];
        if (localConditionGroup.getCondition().evalTableData(paramTableData, new int[] { k }, paramInt, paramCalculator).length > 0)
        {
          arrayOfIntList[i1].add(k);
          l = 1;
          if (!(this.more))
            break;
        }
      }
      if (l == 0)
      {
        try
        {
          localArrayList1.add(paramTableData.getValueAt(k, paramInt));
        }
        catch (TableDataException localTableDataException2)
        {
          FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
          localArrayList1.add(Primitive.NULL);
        }
        localIntList.add(k);
      }
    }
    ArrayList localArrayList2 = new ArrayList();
    for (int k = 0; k < arrayOfIntList.length; ++k)
    {
      localObject = arrayOfIntList[k];
      if ((!(this.force)) && (((IntList)localObject).size() == 0))
        break label413:
      localArrayList2.add(new Group(arrayOfObject[k], ((IntList)localObject).toArray()));
    }
    if ((this.other != 1) && (localIntList.size() > 0))
      switch (this.other)
      {
      case 0:
        localArrayList2.add(new Group(this.odisplay, localIntList.toArray()));
        break;
      case 2:
        label413: FunctionGrouper localFunctionGrouper = new FunctionGrouper();
        localObject = localFunctionGrouper.group(paramTableData, paramInt, localIntList.toArray(), paramCalculator);
        int i2 = 0;
        int i3 = localObject.length;
        while (i2 < i3)
        {
          localArrayList2.add(localObject[i2]);
          ++i2;
        }
      }
    return ((Group)(Group[])localArrayList2.toArray(new Group[localArrayList2.size()]));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if ("Attr".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("other")) != null)
          setOther(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("odisplay")) != null)
          setOdisplay(str1);
        if ((str1 = paramXMLableReader.getAttr("force")) != null)
          setForce(Boolean.valueOf(str1).booleanValue());
        if ((str1 = paramXMLableReader.getAttr("more")) != null)
          setForce(Boolean.valueOf(str1).booleanValue());
      }
      else if ("ConditionListGroup".equals(str2))
      {
        ConditionGroup localConditionGroup = new ConditionGroup();
        paramXMLableReader.readXMLObject(localConditionGroup);
        if (this.conditionGroups == null)
        {
          this.conditionGroups = new ConditionGroup[] { localConditionGroup };
        }
        else
        {
          ConditionGroup[] arrayOfConditionGroup = this.conditionGroups;
          this.conditionGroups = new ConditionGroup[arrayOfConditionGroup.length + 1];
          System.arraycopy(arrayOfConditionGroup, 0, this.conditionGroups, 0, arrayOfConditionGroup.length);
          this.conditionGroups[arrayOfConditionGroup.length] = localConditionGroup;
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    paramXMLPrintWriter.startTAG("Attr");
    paramXMLPrintWriter.attr("other", this.other);
    if ((this.other == 0) && (!(this.odisplay.equals("Others"))))
      paramXMLPrintWriter.attr("odisplay", this.odisplay);
    if (this.force)
      paramXMLPrintWriter.attr("force", this.force);
    if (!(this.more))
      paramXMLPrintWriter.attr("more", this.more);
    paramXMLPrintWriter.end();
    if (this.conditionGroups != null)
      for (i = 0; i < this.conditionGroups.length; ++i)
      {
        paramXMLPrintWriter.startTAG("ConditionListGroup");
        this.conditionGroups[i].writeXML(paramXMLPrintWriter);
        paramXMLPrintWriter.end();
      }
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}